1710A - Color the Picture - CodeForces Solution


constructive algorithms greedy math *1500

Please click on ads to support us..

Python Code:

def canColor(n, m, colors):
    colors.sort(reverse=True)
    n, m = _n, _m = sorted([n, m])
    cando = 0
    mfirst = colors[0]//n
    nfirst = colors[0]//m
    for c in colors:
        if c//n>1:
            if _m==1:
                if mfirst>2:
                    _m = 0 
            else:
                _m -= c//n 
        if c//m>1:
            if _n==1:
                if nfirst>2:
                    _n = 0
            else:
                _n -= c//m 
    if _m<=0 or _n<=0: cando = 1
    return "Yes" if cando else "No"
    

def main():
    t = int(input())
    for _ in range(t):
        n, m, k = map(int, input().split())
        colors = list(map(int, input().split()))
        print(canColor(n, m, colors))

main()

C++ Code:

#include<bits/stdc++.h>

using namespace std;

//Ordered set
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// typedef tree<int,null_type,less<int>,rb_tree_tag,
// tree_order_statistics_node_update> indexed_set;

#define rep(i, x, n) for(ll i = (x); i < (n); i++)
#define ll long long
#define endl "\n"
#define boost ios_base::sync_with_stdio(false);cin.tie(nullptr);
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define vll vector<ll>
#define mll map<ll,ll>
#define pll pair<ll,ll>
#define mp make_pair
#define ckmin(a,b) a = min(a,b)
#define ckmax(a,b) a = max(a,b)
#define inf ( (1e18) + 10)
#define mod 1000000007
#define endll cout<<endl
#define minPQ priority_queue <ll, vector<ll>, greater<ll>>
//getline(cin, s) to take whole input line with spaces
ll power(ll x, ll y)//power using mod
{
    x %= mod, y %= mod - 1;
    ll res = 1;
    while (y)
    {
        if (y & 1LL)
            res = (res * x) % mod;
        y >>= 1;
        x = (x * x) % mod;
    }
    return res % mod;
}

//Sieve of erothrosiene - to find Primes and smallest prime factor(SPF)

// ll range =1e5+7;

// vll primes(range, 1);

// for(int i = 0; i < range; i++)primes[i] = i;

// for(int i = 2; i*i < range; i++){
//     if(primes[i] == i){
//         for(int j = i*i; j < range; j+=i){
//             if(primes[j] == j)
//             primes[j] = i;
//         }
//     }
// }

/* ------------------------------------------------------------------------------------------------*/

void solve(){
    ll n, m, k;
    cin>>n>>m>>k;
    vll a(k);
    rep(i, 0, k)cin>>a[i];

    ll cnt = 0, od = 0, ev = 0;
    for(int i = 0; i < k; i++){
        ll o = a[i]/n;

        if(o != 1){
            cnt += o;
        }

        if(o > 2)od = 1;
    }

    if(!(m&1)){
        if(cnt >= m){
            cout<<"YES"<<endl;
            return;
        }
    }else{
        if(cnt >= m && od){
            cout<<"YES"<<endl;
            return;
        }
    }

    
    cnt = 0; od = 0;
    for(int i = 0; i < k; i++){
        ll o = a[i]/m;

        if(o!=1){
            cnt += o;
        }

        if(o > 2)od = 1;
    }

    if(!(n&1)){
        if(cnt >= n){
            cout<<"YES"<<endl;
            return;
        }
    }else{
        if(cnt >= n && od){
            cout<<"YES"<<endl;
            return;
        }
    }

    cout<<"NO"<<endl;
    
}

int main (){
    boost
    //To take input from files
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout); 

    ll t;
    cin>>t;
    while(t--)
    solve();

}


Comments

Submit
0 Comments
More Questions

13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division